package io.springboot.jpa.examples.springbootjpaexamples.web.controller;

import io.springboot.jpa.examples.springbootjpaexamples.model.Person;
import io.springboot.jpa.examples.springbootjpaexamples.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author yzc
 * @description
 * @date 2018/9/2
 */
@Controller
@RequestMapping("/persons")
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        Page<Person> personPage = personService.findAll(new PageRequest(0, 5));
        model.addAttribute("personPage", personPage);
        return "persons/index";
    }

    @RequestMapping(value = "/{id}")
    @ResponseBody
    public Person getOne(@PathVariable("id") String id) {
        return personService.getOne(Long.valueOf(id));
    }
}
